package com.bsi.login.oauth.entity;



import org.springframework.lang.Nullable;

import javax.validation.constraints.NotNull;
import java.util.Map;

/*
 * Author: liusongs
 * Date: 2019/10/20 19:43
 * Content:请求要传递的参数类,封装了各个情况下访问oauth2工程需要传递的必要参数 ，以下属性需要与oauth工程里RemoteUserDetails，TokenEnhancer配置的属性名相同，不然会导致解析错误
 */
public final class Oauth2ReqParamVo {
    private String username; // 传递oauth工程的用户名

    private String password; //传递oauth工程的密码

    private String userId; // 传递oauth工程的用户id

    private String grant_type; // 请求类型（password,refresh_token等模式）

    private String token; // check模式下的token值 传递给oauth工程tokenValue，刷新token和校验token需要带此致值过去

    private String refresh_token; // refreshToken模式下的token值 传递给oauth工程tokenValue，刷新token和校验token需要带此致值过去

    private Map<String, Object> otherParam; // 其余参数


    private Oauth2ReqParamVo(Oauth2ReqVoBuilder builder) {
        this.username = builder.username;
        this.password = builder.password;
        this.userId = builder.userId;
        this.token = builder.token;
        this.refresh_token = builder.refresh_token;
        this.grant_type = builder.grantType;
        this.otherParam = builder.otherParam;
    }


    public static class Oauth2ReqVoBuilder {
        private String username; // 传递oauth工程的用户名

        private String password; //传递oauth工程的密码

        private String userId; // 传递oauth工程的用户id

        private String token; // check模式下的token值 传递给oauth工程tokenValue，刷新token和校验token需要带此致值过去

        private String refresh_token; // refreshToken模式下的token值 传递给oauth工程tokenValue，刷新token和校验token需要带此致值过去

        private String grantType; // 请求类型（password,refresh_token等模式）

        private Map<String, Object> otherParam; // 其余参数

        /**
         * 像oauth2工程拿到token的请求方式
         *
         * @param username   用户名
         * @param password   密码
         * @param userId     用户主键
         * @param tokenMode  校验类型，此模式为token_mode模式
         * @param otherParam 其余参数
         * @return
         */
        public Oauth2ReqParamVo tokenModeBuiler(@NotNull String username, @NotNull String password, @NotNull String userId, @NotNull OauthVisitModeEnum tokenMode, @Nullable Map<String, Object> otherParam) {
            this.username = username;
            this.userId = userId;
            this.password = password;
            this.grantType = tokenMode.getGrantType();
            this.otherParam = otherParam;
            return new Oauth2ReqParamVo(this);
        }

        /**
         * 刷新Token模式
         *
         * @param refresh_mode  此模式为refresh_mode
         * @param refresh_token refresh_token的值
         * @param otherParam    其余参数
         * @return
         */
        public Oauth2ReqParamVo refreshTokenModeBuiler(@NotNull OauthVisitModeEnum refresh_mode, @NotNull String refresh_token, @Nullable Map<String, Object> otherParam) {
            this.grantType = refresh_mode.getGrantType();
            this.refresh_token = refresh_token;
            this.otherParam = otherParam;
            return new Oauth2ReqParamVo(this);
        }

        /**
         * 此模式为检验token模式
         *
         * @param token token的值
         * @return
         */
        public Oauth2ReqParamVo checkTokenBuilder(@NotNull String token, @Nullable Map<String, Object> otherParam) {
            this.token = token;
            this.otherParam = otherParam;
            return new Oauth2ReqParamVo(this);
        }

        /**
         * 此模式为退出，删除token模式
         *
         * @param token token的值
         * @return
         */
        public Oauth2ReqParamVo deleteTokenBuilder(@NotNull String token, @Nullable Map<String, Object> otherParam) {
            this.token = token;
            this.otherParam = otherParam;
            return new Oauth2ReqParamVo(this);
        }
    }


    public String getGrant_type() {
        return grant_type;
    }

    public void setGrant_type(String grant_type) {
        this.grant_type = grant_type;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getRefresh_token() {
        return refresh_token;
    }

    public void setRefresh_token(String refresh_token) {
        this.refresh_token = refresh_token;
    }

    public Map<String, Object> getOtherParam() {
        return otherParam;
    }

    public void setOtherParam(Map<String, Object> otherParam) {
        this.otherParam = otherParam;
    }
}
